import java.util.Arrays;
import java.util.List;

public class Solution1434 {
    public int numberWays(List<List<Integer>> hats) {
        int mod=1000000007;
        int[][] f=new int[41][1<<hats.size()];
        f[0][0]=1;
        for (int i=1;i<=40;i++){
            for (int j=0;j<1<<hats.size();j++){
                f[i][j]=f[i-1][j];
                for (int k=0;k<hats.size();k++){
                    if ((j&(1<<k))>0){
                        List<Integer> list=hats.get(k);
                        boolean flag=false;
                        for (int ii : list) {
                            if (ii == i) {
                                flag = true;
                                break;
                            }
                        }
                        if (flag){
                            f[i][j]=(f[i][j]+f[i-1][j^(1<<k)])%mod;
                        }
                    }
                }
            }
        }
        return f[40][(1<<hats.size())-1];
    }

    public static void main(String[] args) {
        System.out.println(new Solution1434().numberWays(Arrays.asList(Arrays.asList(3,5,1),Arrays.asList(3,5))));
    }
}
